﻿using System;
using System.Collections.Generic;
using NmbpProject2.DataLayer;
using NmbpProject2.DboClasses;

namespace NmbpProject2.BusinessLayer
{
    internal class ProgLanguageBL
    {
        public ProgLanguage findByName(String name)
        {
            ProgLanguageDL pDL = new ProgLanguageDL();
            ProgLanguage language = pDL.findByName(name);
            return language;
        }

        public void insertLanguage(string name)
        {
            ProgLanguageDL pDL = new ProgLanguageDL();
            ProgLanguage existingLanguage = pDL.findByName(name);
            try
            {
                if (existingLanguage != null)
                    throw new Exception("There already exists a language with that name!");
                else
                {
                    pDL.insertLanguage(name);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }

        public List<ProgLanguage> getAllLangs()
        {
            ProgLanguageDL pDL = new ProgLanguageDL();
            List<ProgLanguage> langs = pDL.getAllLangs();
            pDL = null;
            return langs;
        }

        public void delete(ProgLanguage language, bool checkCascade)
        {
            ProgLanguageDL pDL = new ProgLanguageDL();
            try
            {
                CompApplicationDL caDL = new CompApplicationDL();
                CompetitionDL coDL = new CompetitionDL();
                List<CompApplication> applications = caDL.findByLanguage(language, false);
                List<Competition> competitions = coDL.findByLanguage(language, false);
                if (applications == null && competitions == null)
                {
                    pDL.delete(language);
                }
                else
                {
                    if (checkCascade)
                    {
                        string message = "There are ";
                        if (applications != null)
                        {
                            message = String.Concat(message, "competition applications");
                        }
                        if (applications != null && competitions != null)
                        {
                            message = String.Concat(message, " and ");
                        }
                        if (competitions != null)
                        {
                            message = String.Concat(message, "competitions");
                        }
                        message = String.Concat(
                            message,
                            " in the database using the selected programming langauge(s)! Some might be deleted!"
                            );
                        throw new Exception(message);
                    }
                    else
                    {
                        // delete apps with single lang
                        List<CompApplication> appsToDel = caDL.findByLanguage(language, true);
                        if (appsToDel != null)
                        {
                            foreach (CompApplication app in appsToDel)
                            {
                                caDL.delete(app);
                                applications.Remove(app);
                            }
                        }
                        // delete comps with single lang
                        List<Competition> compsToDel = coDL.findByLanguage(language, true);
                        if (compsToDel != null)
                        {
                            foreach (Competition comp in compsToDel)
                            {
                                coDL.deleteCompetition(comp);
                                competitions.Remove(comp);
                            }
                        }
                        // remove lang from apps with more than one lang
                        if (applications != null)
                        {
                            foreach (CompApplication app in applications)
                            {
                                app.ProgLanguages.Remove(language);
                                caDL.update(app);
                            }
                        }
                        // remove lang from comps with more than one lang
                        if (competitions != null)
                        {
                            foreach (Competition comp in competitions)
                            {
                                comp.ProgLanguages.Remove(language);
                                coDL.updateCompetition(comp.Name, comp.Date, comp.Categories, comp.ProgLanguages, comp);
                            }
                        }
                        pDL.delete(language);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public void delete(List<ProgLanguage> langs, bool checkCascade)
        {
            ProgLanguageDL pDL = new ProgLanguageDL();
            foreach (ProgLanguage language in langs)
            {
                delete(language, checkCascade);
            }
            pDL = null;
        }

        public void update(ProgLanguage language, string name)
        {
            ProgLanguageDL pDL = new ProgLanguageDL();
            ProgLanguage existingLanguage = pDL.findByName(name);
            try
            {
                if (existingLanguage != null)
                    throw new Exception("There already exists a language with that name!");
                else
                {
                    pDL.update(language, name);
                }
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
    }
}